﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace VersionManage.Common
{
    public class XmlHelper
    {
        /// <summary>
        /// 把List转为XDocument
        /// </summary>
        /// <typeparam name="T">要转换的类型</typeparam>
        /// <param name="root">根目录</param>
        /// <param name="childNode">子节点</param>
        /// <param name="IL">数据列表</param>
        public static XDocument GenerateXml<T>(string root, string childNode, IList<T> IL)
        {
            XDocument doc = new XDocument(
                new XElement(root,
                    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                    new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema")
                )
            );
            doc.Element(root).Add(GetXmlList(IL, childNode));
            return doc;
        }

        public static XDocument GenerateXml<T>(string root, string listNode, string childNode, string command, IList<T> IL)
        {
            XDocument doc = new XDocument(
                new XElement(root,
                    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                    new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"),
                   new XElement("Command", command),
                    new XElement("ClientTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                    new XElement("Result", 0),
                    new XElement("Msg", null)
                )
            );
            if (null != IL && IL.Count > 0)
            {
                XElement list = new XElement("List", GetXmlList(IL, childNode));
                doc.Element(root).Add(list);
            }
            return doc;
        }

        /// <summary>
        /// 请求错误返回的XML文件
        /// </summary>
        /// <param name="errorMsg">具体错误信息</param>
        /// <returns></returns>
        public static XDocument GenerateErrorXml(string root, string command, int result, string errorMessage)
        {
            XDocument doc = new XDocument(
                new XElement(root,
                    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                    new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"),
                    new XElement("Command", command),
                    new XElement("ClientTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                    new XElement("Result", result),
                    new XElement("Msg", errorMessage)
                )
            );
            return doc;
        }

        public static XDocument GenerateErrorXml(string root, string childNode, string command, string errorMessage)
        {
            XDocument doc = new XDocument(
                new XElement(root,
                    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                    new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema")
                )
            );
            XElement xe = new XElement(childNode,
                    new XElement("Command", command),
                     new XElement("ClientTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                    new XElement("Result", 1),
                    new XElement("Msg", errorMessage)
                    );
            doc.Element(root).Add(xe);
            return doc;
        }

        /// <summary>
        /// 实体列表转为XElement列表
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="IL">列表</param>
        /// <param name="Node">节点</param>
        public static List<XElement> GetXmlList<T>(IList<T> IL, string Node)
        {
            List<XElement> listElement = new List<XElement>();
            T obj = Activator.CreateInstance<T>();
            Type type = obj.GetType();
            PropertyInfo[] pis = type.GetProperties();
            for (int i = 0; i < IL.Count; i++)
            {
                XElement xe = new XElement(Node);
                for (int j = 0; j < pis.Length; j++)
                {
                    Type elementType = pis[j].PropertyType;
                    object elementValue = pis[j].GetValue(IL[i], null);
                    if (elementType.Namespace == "System")
                    {
                        XElement x = new XElement(pis[j].Name, pis[j].GetValue(IL[i], null));
                        xe.Add(x);
                    }
                }
                listElement.Add(xe);
            }
            return listElement;
        }

        /// <summary>
        /// 转换属性值
        /// </summary>
        public static string getElementPropertyName(string propertyFullName)
        {
            string elementPropertyName = string.Empty;
            if (elementPropertyName.IndexOf("System.Nullable") >= 0)
            {

                return "";
            }
            else
                return propertyFullName;
        }


        /// <summary>
        /// 将xml反序列化为实体类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xmlDoc"></param>
        /// <returns></returns>
        public static T XMLDeserialize<T>(string type)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            T t;
            try
            {
                using (StringReader reader = new StringReader(type))
                {
                    t = (T)serializer.Deserialize(reader);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return t;
        }

    }
}
